package com.blockchain.securewallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.model.db.BlockAddress;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressItem>{

    private final static String TAG = "CoinWalletAdapter";

    private final LayoutInflater  mLayoutInflater;
    private Context context;

    private List<BlockAddress> addresses = null;
    private IOnClickListener listener;

    public void setListener(IOnClickListener listener) {
        this.listener = listener;
    }

    public AddressAdapter(Context context, List<BlockAddress> addresses) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.addresses = addresses;
    }

    public void setAddresses(List<BlockAddress> addresses) {
        this.addresses = addresses;
    }

    @NonNull
    @Override
    public AddressItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AddressAdapter.AddressItem(mLayoutInflater.inflate(R.layout.address_book_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AddressItem holder, int position) {
        BlockAddress address = addresses.get(position);
        if (address.getType() == BlockAddress.TYPE_BIT_CON) {
            holder.ivIcon.setImageResource(R.drawable.btc);
            holder.tvName.setText(R.string.bit_coin_name);
        }else {
            holder.ivIcon.setImageResource(R.drawable.eth);
            holder.tvName.setText(R.string.eth_coin_name);
        }
        holder.tvNote.setText(address.getNote()==null?"":address.getNote());
        holder.tvAddress.setText(address.getAddress());
        holder.type = address.getType();
    }

    @Override
    public int getItemCount() {
        return addresses==null?0:addresses.size();
    }

    class AddressItem extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;

        @BindView(R.id.tv_name)
        TextView tvName;

        @BindView(R.id.tv_note)
        TextView tvNote;

        @BindView(R.id.tv_address)
        TextView tvAddress;

        int type;

        public AddressItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener!=null) {
                listener.onClick(type,tvAddress.getText().toString(),tvNote.getText().toString());
            }
        }
    }

    public interface IOnClickListener {
        void onClick(int type,String address,String note);
    }
    
}
